#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
检查导入是否正常的测试脚本
"""

import sys
import os

def check_imports():
    """检查所有必要的导入"""
    print("检查导入...")
    print("=" * 50)
    
    try:
        # 检查基础库
        import json
        print("✓ json 导入成功")
        
        import pandas as pd
        print("✓ pandas 导入成功")
        
        from sqlalchemy import create_engine, text, distinct, and_, select
        print("✓ sqlalchemy 导入成功")
        
        from fastapi import FastAPI, HTTPException, Request
        print("✓ fastapi 导入成功")
        
        from fastapi.middleware.cors import CORSMiddleware
        print("✓ fastapi.middleware.cors 导入成功")
        
        from pydantic import BaseModel
        print("✓ pydantic 导入成功")
        
        from typing import Optional, List
        print("✓ typing 导入成功")
        
        # 检查项目内部模块
        sys.path.append(os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))
        
        from config import create_db_engine
        print("✓ config.create_db_engine 导入成功")
        
        from common.rule_flatten_handle import flatten_json, unflatten_json
        print("✓ common.rule_flatten_handle 导入成功")
        
        # 测试数据库连接
        engine = create_db_engine()
        print("✓ 数据库引擎创建成功")
        
        # 测试FastAPI应用导入
        from rule_maker_app import app
        print("✓ rule_maker_app.app 导入成功")
        
        print("\n" + "=" * 50)
        print("✅ 所有导入检查通过！")
        return True
        
    except ImportError as e:
        print(f"❌ 导入错误: {e}")
        return False
    except Exception as e:
        print(f"❌ 其他错误: {e}")
        return False

if __name__ == "__main__":
    success = check_imports()
    if success:
        print("\n🚀 可以安全启动服务")
    else:
        print("\n⚠️  请修复导入问题后再启动服务")
    
    sys.exit(0 if success else 1)